package bean;

import java.io.Serializable;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Date;

import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.*;
import javax.faces.context.FacesContext;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import controller.AmministrazioneInterface;
import dto.ElementoBaseDTO;
import dto.EscursioneDTO;
import dto.HotelDTO;
import dto.VoloDTO;

@ManagedBean(name="elementBean")
@ViewScoped
public class ElementBean implements Serializable {
	private static final long serialVersionUID = 1L;

	@EJB
	private AmministrazioneInterface admin;
	
	private ElementoBaseDTO element;
	private VoloDTO volo;
	private EscursioneDTO escursione;
	private HotelDTO hotel;
	private String tipo;
	
	@Resource(lookup = "EMailME")
	private Session mailSession;//
	
	private boolean visible;
	
	public ElementBean() {
		element = new ElementoBaseDTO();
		volo = new VoloDTO();
		escursione = new EscursioneDTO();
		hotel = new HotelDTO();
	}
	
	public ElementoBaseDTO getElement() {
		return element;
	}

	public void setElement(ElementoBaseDTO element) {
		this.element = element;
	}
	

	public boolean isVisible() {
		return visible;
	}

	public void setVisible(boolean visible) {
		this.visible = visible;
	}

	public String crea() {
		admin.creaElementoBase(element);
		return "index";
	}
	
	public String creaVolo(){
		element.setTipo("Volo");
		admin.creaVolo(element,volo);
		return "index";
	}
	
	public String creaHotel(){
		element.setTipo("Hotel");
		admin.creaHotel(element,hotel);
		return "index";
	}
	
	public String checkTime(){
		if(escursione!=null) {
			String sof = escursione.getOraFine();
			sof+=":00";
			String soi = element.getOra();
			soi+=":00";
			Time of = Time.valueOf(sof);
			Time oi = Time.valueOf(soi);
			if(of.before(oi)){
		        FacesContext facesContext = FacesContext.getCurrentInstance();  
		        facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Impossibile creare l'escursione, le ore indicate non sono consistenti!","" ));
				return "";
			}
			else
				return creaEscursione();
		}
		return "";
	}
	
	public String creaEscursione(){
		element.setTipo("Escursione");
		admin.creaEscursione(element,escursione);
		return "index";
	}
	
	public String getTipo() {
		return tipo;
	}

	public void setTipo(String tipo) {
		this.tipo = tipo;
	}

	public VoloDTO getVolo() {
		return volo;
	}

	public void setVolo(VoloDTO volo) {
		this.volo = volo;
	}

	public EscursioneDTO getEscursione() {
		return escursione;
	}

	public void setEscursione(EscursioneDTO escursione) {
		this.escursione = escursione;
	}

	public HotelDTO getHotel() {
		return hotel;
	}

	public void setHotel(HotelDTO hotel) {
		this.hotel = hotel;
	}
	
	public String elimina(final ElementoBaseDTO selectedElemento){
		final ArrayList<String> email = admin.eliminaElementoBase(selectedElemento);
		Thread t = new Thread(new Runnable() {
	         public void run()
	         {
	        	ArrayList<String> sending = new ArrayList<String>();

	        		for(String e : email){
	        			boolean ins = true;
	        			for(String s: sending){
	        				if(s==e) ins=false;
	        			}
	        			if(ins) sending.add(e);
	        		}
	    		for(String x : sending){
	    			System.out.println(x);
	    			sendEmail(x, "Eliminazione", "La informiamo che l'elemento "+selectedElemento.getNome()+" è stato eliminato");
	    		}
	         }
	});
		t.start();
		return "index?faces-redirect=true";
	}
	
	
public void sendEmail(String to, String subject, String body) {
		
		MimeMessage message = new MimeMessage(mailSession);
		try {
		message.setFrom(new InternetAddress(mailSession.getProperty("mail.from")));
		InternetAddress[] address = {new InternetAddress(to)};
		message.setRecipients(MimeMessage.RecipientType.TO, address);
		message.setSubject(subject);
		message.setSentDate(new Date());
		message.setText(body);
		Transport.send(message);
		} catch (MessagingException ex) {
		ex.printStackTrace();
		}
		}
  
}
